package com.wawei.seeds.services;

import android.content.Context;
import android.os.Handler;

import com.wawei.seeds.model.ExpertInfo;
import com.wawei.seeds.services.remote.ExpertInfoRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutExpertRestfulService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.RetrofitNetworkService;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by itachi on 15/7/20.
 */
public class WalnutExpertServiceHandler {
    private Context context;

    public WalnutExpertServiceHandler(Context context) {
        this.context = context;
    }

    public void registerExpert(RestAdapter restAdapter, final Integer userId, final String name,
                               final String desc, final String tag, final Handler handler, final Integer handlerTag) {
        WalnutExpertRestfulService service = restAdapter.create(WalnutExpertRestfulService.class);
        service.reservation(userId, name, desc, tag, "", new Callback<NetBase>(){

            @Override
            public void success(NetBase netBase, Response response) {
                try {
                    if (netBase.getResultCode() == RetrofitNetworkService.SUCCESS) {
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, "ok");
                    }
                    else {
                        RetrofitNetworkService.handleError(handler, handlerTag, null);
                    }
                }
                catch (Exception e) {
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, null);
            }
        });
    }

    public void checkExpert(RestAdapter adapter, final String expertId, final Handler handler, final Integer handlerTag){
        WalnutExpertRestfulService service = adapter.create(WalnutExpertRestfulService.class);
        service.getExpertInfo(expertId, new Callback<ExpertInfoRestfulWrapper>() {
            @Override
            public void success(ExpertInfoRestfulWrapper expertInfoWrapper, Response response) {
                if (null != expertInfoWrapper && expertInfoWrapper.getResultCode() == RetrofitNetworkService.SUCCESS) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, expertInfoWrapper.getProvider());
                } else {
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
            }
        });
    }

    public ExpertInfo checkExpert(RestAdapter adapter, final String expertId){
        WalnutExpertRestfulService service = adapter.create(WalnutExpertRestfulService.class);
        ExpertInfoRestfulWrapper expertWrapper = service.getExpertInfo(expertId);
        return expertWrapper.getProvider();
    }

}
